package com.liruo.demojsrjpa.model.entity;

import java.math.BigDecimal;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import lombok.Data;

/*
create table auto_mapping_entity (
id bigint not null auto_increment,
i11 varchar(128) not null,
i12 varchar(128) not null,
long_val decimal(20,5),
simple_val bigint default 520,
text text not null,
uk11 varchar(128) not null,
uk12 varchar(128) not null,
uk21 varchar(128) not null,
uk22 varchar(128) not null,
primary key (id)
) engine=InnoDB;

create index i1 on auto_mapping_entity (i11, i12);
alter table auto_mapping_entity drop index uk1
alter table auto_mapping_entity add constraint uk1 unique (uk11, uk12)
alter table auto_mapping_entity drop index uk2
alter table auto_mapping_entity add constraint uk2 unique (uk21, uk22)
 */
@Data
@Entity
@Table(
    uniqueConstraints = {
        @UniqueConstraint(name = "uk1", columnNames = {"uk11", "uk12"})
    },
    indexes = {
        @Index(name = "i1", columnList = "i11,i12", unique = false),
        @Index(name = "uk2", columnList = "uk21,uk22", unique = true)
    }
)
public class AutoMappingEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(unique = false)
  private Long id;
  @Column(nullable = false, length = 128)
  private String uk11;
  @Column(nullable = false, length = 128)
  private String uk12;
  @Column(nullable = false, length = 128)
  private String uk21;
  @Column(nullable = false, length = 128)
  private String uk22;
  @Column(nullable = false, length = 128)
  private String i11;
  @Column(nullable = false, length = 128)
  private String i12;
  @Column(nullable = true, precision = 20, scale = 5)
  private BigDecimal decimalVal;

  //columnDefinition仅包含除列名之外的, 不能在列定义上同时定义其他同样的定义,因为其他的会被追加到columnDefinition尾部
  //  对于默认值，需要注意save时通常会设置所有字段，所以如果不想自己写sql，就同时给字段默认值
  @Column(columnDefinition = "bigint default 520", nullable = true)
  private Long simpleVal = 520L;

  //默认是最大的Lob, Blob是longBlob, text是longText, 只能使用columnDefinition进行定义
  @Basic(fetch = FetchType.LAZY)
  @Lob
  @Column(columnDefinition = "text", nullable = false)
  private String textVal;

  public AutoMappingEntity() {
  }

  public AutoMappingEntity(
      String uk11, String uk12,
      String uk21, String uk22,
      String i11, String i12,
      BigDecimal decimalVal, String textVal
  ) {
    this.uk11 = uk11;
    this.uk12 = uk12;
    this.uk21 = uk21;
    this.uk22 = uk22;
    this.i11 = i11;
    this.i12 = i12;
    this.decimalVal = decimalVal;
    this.textVal = textVal;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    AutoMappingEntity entity = (AutoMappingEntity) o;

    if (!Objects.equals(id, entity.id)) {
      return false;
    }
    if (!Objects.equals(uk11, entity.uk11)) {
      return false;
    }
    if (!Objects.equals(uk12, entity.uk12)) {
      return false;
    }
    if (!Objects.equals(uk21, entity.uk21)) {
      return false;
    }
    if (!Objects.equals(uk22, entity.uk22)) {
      return false;
    }
    if (!Objects.equals(i11, entity.i11)) {
      return false;
    }
    if (!Objects.equals(i12, entity.i12)) {
      return false;
    }

    if (decimalVal == null && entity.decimalVal != null
        || decimalVal != null && entity.decimalVal == null
        || decimalVal != null && entity.decimalVal != null
        && decimalVal.compareTo(entity.decimalVal) != 0
    ) {
      return false;
    }
    if (!Objects.equals(simpleVal, entity.simpleVal)) {
      return false;
    }
    return Objects.equals(textVal, entity.textVal);
  }

  @Override
  public int hashCode() {
    int result = id != null ? id.hashCode() : 0;
    result = 31 * result + (uk11 != null ? uk11.hashCode() : 0);
    result = 31 * result + (uk12 != null ? uk12.hashCode() : 0);
    result = 31 * result + (uk21 != null ? uk21.hashCode() : 0);
    result = 31 * result + (uk22 != null ? uk22.hashCode() : 0);
    result = 31 * result + (i11 != null ? i11.hashCode() : 0);
    result = 31 * result + (i12 != null ? i12.hashCode() : 0);
    result = 31 * result + (decimalVal != null ? decimalVal.hashCode() : 0);
    result = 31 * result + (simpleVal != null ? simpleVal.hashCode() : 0);
    result = 31 * result + (textVal != null ? textVal.hashCode() : 0);
    return result;
  }
}
